package com.cgbl.service.vm

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant.CHAT_ID_KRY
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant.CHAT_KRY
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant.KEY_SESSION_TYPE
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant.PATH_CHAT_P2P_PAGE
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant.PATH_CHAT_SEARCH_PAGE
import com.netease.yunxin.kit.corekit.route.XKitRouter.withKey
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.BaseResponse
import com.robot.base.model.UserInfoBean
import com.robot.base.util.ActivityUtils
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand

class PersonalProfileViewModel(application: Application) : GLBaseViewModel(application) {

    val accountIdField = ObservableField<String>()

    val accountNameField = ObservableField<String>()

    val accountJobField = ObservableField<String>()

    val accountAvatarField = ObservableField<String>()

    val accountPhoneField = ObservableField<String>()

    val accountSignField = ObservableField<String>()

    var accid = ""

    val doSendMessageCommand = BindingCommand<Any>(BindingAction {
        sendMessage()
    })

    fun fetchUserInfo(id: String) {
        UserServiceFactory.otherUserInfo(id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<UserInfoBean>>(this) {
                override fun onNext(t: BaseResponse<UserInfoBean>) {
                    accountIdField.set("ID:${t.data.id}")
                    accountNameField.set(t.data.nickName)
                    accountJobField.set(t.data.career)
                    accountAvatarField.set(t.data.head)
                    accountPhoneField.set("手机号：${t.data.mobile}")
                    accountSignField.set("个性签名：${t.data.userSign}")
                    accid = t.data.accid ?: ""
                }
            })
    }

    fun sendMessage() {
        if (TextUtils.isEmpty(accid)) return
        withKey(PATH_CHAT_P2P_PAGE).withParam(CHAT_ID_KRY, accid)
            .withContext(ActivityUtils.getTopActivity()).navigate()
    }
}